export const img_root = 'http://img.rcglife.com/';

// 图片
export function handPic(url, size) {
    let _url;
    if (url) {
        _url = img_root + url + '-r' + (parseInt(size) * 100);
    }
    return _url;
};

// 时间格式化
export function timeFormat(time, format = "hi") {
    let date = new Date(parseInt(time) * 1000),
        minute = date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes();
    switch (format) {
        case 'hi':
            return date.getHours() + ":" + minute;
            break;
        case 'ymd':
            return date.getFullYear() + "-" + date.getMonth() + "-" + date.getDate();
            break;
    }
};

// 页面跳转 参数 
// url==>需要跳转页面的名称,
// tier需要出文件夹的层数, 默认是一层../
// param传递的参数
export function navigateTo(name,param="", tier = 1){
    let path = '', url = name + "/" + name;

    for(let i =1;i<=tier;i++){ 
        path += "../" 
    };

    url = path + name + "/" + name + param;
    wx.navigateTo({ url: url });
};

// 数组/对象 ==> 转字符串 需要参数 genre =[]/{};
// 必须传参数
export function shiftString(genre){
    let _type,result; // 获取参数类型

    if (genre instanceof Array){
        _type="Array"
    } else if (genre instanceof Object){
        _type = "Object"
    } else if (typeof genre=='undefined'){
        console.log('必须传递参数')
        return false;
    };
    // 做相应的处理
    switch (_type){
        case "Array":
            result = genre.join(',');
            break;
        case "Object":
            result = JSON.stringify(genre);  
            break;
    };
    
    return result;
};

// 倒计时 参数
// 现在时间戳 cur_ctime 
// 结束时间戳 over_ctime
export function countDown(cur_ctime, over_ctime){
    // 检测是否具备两个参数
    if (cur_ctime && over_ctime){
        //   参数类型是否为string/number
        let cur_type = typeof cur_ctime; 
        let over_type = typeof over_ctime;
        
        let cur_status = cur_type == 'number' || cur_type == 'string';
        let over_status = over_type == 'number' || over_type == 'string';

        if (cur_status && over_status){ 
            // 类型转换
            let _cur_ctime, _over_ctime,diff_time;
            _cur_ctime = parseInt(cur_ctime);
            _over_ctime = parseInt(over_ctime);
            
            diff_time = _over_ctime - _cur_ctime;
            // 倒计时暂时支持 时=分-秒
            let h, m, s;
            let timer = setInterval(_=>{
                
                h = parseInt(diff_time /3600);
                m = parseInt((diff_time - (h * 3600)) / 60);
                s = diff_time - (h * 3600)-(m*60);

                m < 10 ? m = '0' + m : null;
                s < 10 ? s = '0' + s : null;

                console.log(h, m, s);
                diff_time--;
                if(h==0&&m=="00"&&s=='00'){
                    clearInterval(timer);
                }
                return {h:h,m:m,s:s}
            },1000);
        }else{
            console.log("参数类型必须是string或者number类型")
        }
    }else{
        console.log("缺少参数")
    };
}
